package com.sellerSystem.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sellerSystem.dao.SellerDao;
import com.sellerSystem.vo.Commodity;
import com.sellerSystem.vo.Seller;

@Repository("sd")
public class SellerDaoImpl implements SellerDao {
	@PersistenceContext(name = "un")
	private EntityManager em;

	@Override
	@Transactional
	public int addSeller(Seller seller) {
		// TODO Auto-generated method stub
		em.persist(seller);
		return seller.getSellerId();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Seller findSeller(String sellerTel, String sellerPsd) {
		// TODO Auto-generated method stub
		String jpql = "select s from Seller s where s.sellerTel=:sellerTel and s.sellerPsd=:sellerPsd";
		List<Seller> ls = em.createQuery(jpql)
				.setParameter("sellerTel", sellerTel)
				.setParameter("sellerPsd", sellerPsd).getResultList();

		// System.out.println(ls.isEmpty()+"!!!!!!!!!!");
		if (ls.isEmpty())
			return null;
		else
			return ls.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Boolean findTel(String sellerTel) {
		// TODO Auto-generated method stub
		String jpql = "select s from Seller s where s.sellerTel=:sellerTel";
		List<Seller> s = em.createQuery(jpql)
				.setParameter("sellerTel", sellerTel).getResultList();
		if (s.isEmpty())
			return false;
		else
			return true;
	}

	@Override
	@Transactional
	public int modifyPsd(String sellerTel, String sellerPsd) {
		// TODO Auto-generated method stub
		String jpql = "update Seller s set s.sellerPsd=:sellerPsd where s.sellerTel=:sellerTel";
		int rs = em.createQuery(jpql).setParameter("sellerTel", sellerTel)
				.setParameter("sellerPsd", sellerPsd).executeUpdate();
		return rs;

	}

	@Override
	@Transactional
	public int addCommodity(Commodity commodity) {
		// TODO Auto-generated method stub
		em.persist(commodity);
		return commodity.getCommodityId();
	}

	@Override
	@Transactional
	public int modifyCommodity(Commodity commodity) {
		// TODO Auto-generated method stub
		/*String jpql = "delete from Commodity c where c.commodityId=:commodityId";
		em.createQuery(jpql).setParameter("commodityId", commodityId)
				.executeUpdate();
		em.persist(commodity);*/
		String jpql="update Commodity sp set sp.commodityId=:commodityId,"
				+ "sp.commodityName=:commodityName,sp.commodityPic=:commodityPic,"
				+ "sp.commodityPrice=:commodityPrice,sp.commodityKinds=:commodityKinds,"
				+ "sp.commodityGuaranty=:commodityGuaranty,sp.commodityNumber=:commodityNumber,"
				+ "sp.commodityDescribe=:commodityDescribe where sp.commodityId=:commodityId";
		em.createQuery(jpql)
				.setParameter("commodityId", commodity.getCommodityId())
				.setParameter("commodityName",commodity.getCommodityName())
				.setParameter("commodityPic",commodity.getCommodityPic())
				.setParameter("commodityPrice",commodity.getCommodityPrice())
				.setParameter("commodityKinds",commodity.getCommodityKinds())
				.setParameter("commodityGuaranty",commodity.getCommodityGuaranty())
				.setParameter("commodityNumber",commodity.getCommodityNumber())
				.setParameter("commodityDescribe",commodity.getCommodityDescribe())
				.executeUpdate();
				//.getResultList();
		return commodity.getCommodityId();
	}

	@Override
	@Transactional
	public int deleteCommodity(Commodity commodity) {
		String jpql = "delete from Commodity c where c.commodityId=:commodityId";
		int rs = em.createQuery(jpql).setParameter("commodityId", commodity.getCommodityId())
				.executeUpdate();
		return rs;
	}

	@Override
	public Commodity queryCommodity(Integer commodityId) {
		// TODO Auto-generated method stub
		String jpql = "select c from Commodity c where c.commodityId=:commodityId";
		@SuppressWarnings("unchecked")
		List<Commodity> lc = em.createQuery(jpql)
				.setParameter("commodityId", commodityId).getResultList();
		return lc.get(0);
	}

	@Override
	public String accounts() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void checkEvaluation() {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	@Override
	public  ArrayList<Commodity> findCommodity(String sellerName) {
		// TODO Auto-generated method stub
		String jpql = "select c from Commodity c where c.sellerName=:sellerName";
		 ArrayList<Commodity> lc = (ArrayList<Commodity>) em.createQuery(jpql)
				.setParameter("sellerName", sellerName).getResultList();
		return lc;

	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Seller> seller() {
		// TODO Auto-generated method stub
		String jpql = "select s from Seller s";
		ArrayList<Seller> ls =(ArrayList<Seller>) em.createQuery(jpql).getResultList();
		return ls;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Seller findId(int sellerId) {
		// TODO Auto-generated method stub
		String jpql = "select s from Seller s where s.sellerId=:sellerId";
		List<Seller> ls = (List<Seller>) em.createQuery(jpql).setParameter("sellerId", sellerId).getResultList();
		return ls.get(0);
	}

}
